package cn.com.gridinfo.sooc.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import cn.com.gridinfo.sooc.SoocApplication;
import cn.com.gridinfo.sooc.base.http.SoocNetResult;
import cn.com.gridinfo.sooc.common.Const;
import cn.com.gridinfo.sooc.core.course.pay.activity.PayResultActivity;
import cn.com.gridinfo.sooc.core.course.pay.common.CodeConst;
import cn.com.gridinfo.sooc.core.course.pay.dao.PayDao;
import cn.com.gridinfo.sooc.utils.IntentUtil;
import cn.com.gridinfo.sooc.utils.MessageUtils;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler, SoocNetResult {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
    private PayDao payDao;//支付dao
    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SoocApplication.getContext().addActivity(this);
        api = WXAPIFactory.createWXAPI(this, Const.APP_ID_WX);
        api.handleIntent(getIntent(), this);
        payDao = new PayDao(this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    /**
     * 支付之后回掉
     *
     * @param resp
     */
    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {

            switch (resp.errCode) {
                case 0:
                    //成功
                    IntentUtil.intentSlidIn(this, PayResultActivity.class, 00);
                    finish();
                    break;
                case -1:
                    //交易失败
                    MessageUtils.showImageShortToast(this, "交易失败");
                    finish();
                    break;
                case -2:
                    //用户取消
                    finish();
                    break;
            }
        }
    }

    @Override
    public void onRequestSuccess(int requestCode) {
        //回调接口成功
        if (requestCode == CodeConst.PAY_RESULT) {
            if (payDao.getStatus().equals("0")) {
                //数据更新成功
                MessageUtils.showRightImageShortToast(this, "支付成功");
                IntentUtil.intentSlidIn(this, PayResultActivity.class, 00);
                finish();
            } else {
                //数据更新失败
                MessageUtils.showImageLongToast(this, payDao.getStatusCode());
            }
        }
    }

    @Override
    public void onRequestFaild(int requestCode, String errorNo, String errorMessage) {
        finish();
    }

    @Override
    public void onNoConnect() {
        finish();
    }

    @Override
    public void onInvalidLogin() {
        finish();
    }
}